package com.zx;

import com.ali.goods.domain.UserDM;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest
public class Demo1 {

    @Test
    public void demo01() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list.add(1);
        list.add(2);
        System.out.println("原集合为：" + list);
        List<Integer> collect = list.stream()
                .distinct()
                .collect(Collectors.toList());
        System.err.println("去重后的集合为：" + collect);

        List<Integer> collect1 = list.stream()
                .limit(5)
                .collect(Collectors.toList());
        System.err.println("返回前5个元素的集合为：" + collect);

        List<Integer> collect2 = list.stream()
                .skip(5)
                .collect(Collectors.toList());
        System.err.println("跳过第5个元素的集合为：" + collect2);
//        //stream流的filter方法
//        //filter是过滤操作，返回结果为true的数据
//        List<Integer> fList = list.stream()
//                .filter(i -> i <= 5)
//                .collect(Collectors.toList());
//        System.err.println(fList);
//        //strean流的map方法
//        //将流的每一个数据映射
//        List<Integer> mapList = list.stream()
//                .map(i -> i * 2)
//                .collect(Collectors.toList());
//        System.err.println(mapList);
    }

    @Test
    public void demo02() {
        List<UserDM> userDMS = new ArrayList<>();
        UserDM userDM1 = new UserDM(1, "张三", 19);
        userDMS.add(userDM1);
        UserDM userDM2 = new UserDM(2, "李四", 18);
        userDMS.add(userDM2);
        UserDM userDM3 = new UserDM(3, "王五", 21);
        userDMS.add(userDM3);
        UserDM userDM4 = new UserDM(4, "赵六", 17);
        userDMS.add(userDM4);
        System.out.println("用户列表原顺序：" + userDMS);

        List<UserDM> userDMS1 = userDMS.stream()
                .sorted(Comparator.comparing(UserDM::getAge))
                .collect(Collectors.toList());
        System.err.println("通过age排序后：" + userDMS1);

        UserDM userDM5 = new UserDM(5, "田七");
        userDMS.add(userDM5);
        List<UserDM> collect = userDMS.stream()
                .sorted(Comparator.comparing(
                        UserDM::getAge,
                        //将空值放在最后
                        Comparator.nullsLast(Integer::compareTo)
                        //将空值放在最前
                        //Comparator.nullsFirst(Integer::compareTo)
                ))
                .collect(Collectors.toList());
        System.out.println("含空值的正确排序：" + collect);

        userDMS.parallelStream()
                .forEach(userDM -> {
                    if(userDM.getAge()!=null&&userDM.getAge()>20){
                        System.out.println(userDM);
                    }
                });

    }

    //创建MyRunnable类的对象
    class MyRunable implements Runnable{

        @Override
        public void run() {
            System.out.println("多线程程序启动了");
        }
    }

    @Test
    public void test01(){
        //实现类的方式实现需求
        MyRunable myRunable = new MyRunable();
        //创建Thread类的对象，把MyRunable的对象作为构造参数传递
        Thread thread = new Thread(myRunable);
        //启动线程
        thread.start();
    }

    @Test
    public void test02(){
        //匿名内部类的方式改进
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程启动2");
            }
        }).start();
    }

    @Test
    public void test03(){
        //lambda表达式方式改进
        new Thread(() ->{
            System.out.println("线程3启动了");
        }).start();
    }







}
